/*
 * @Author: ws
 * @Date: 2024-05-27 15:24:02
 * @email: 458658538@qq.com
 * @LastEditors: ws
 * @LastEditTime: 2024-05-27 15:26:54
 * @Description: 
 */
const path = require("path");
const fs = require("fs");
const { defineConfig } = require("@vue/cli-service");
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const { toDateString } = require("xe-utils");
const cdnDependencies = require("./dependencies.cdn");
const activitiConfig = require("./activiti.config");

const smp = new SpeedMeasurePlugin({
  outputFormat: "human",
});

process.env.VUE_APP_VERSION = toDateString(new Date(), "yyyyMMddHHmmss");

module.exports = defineConfig({
  publicPath: "",
  assetsDir: "static",
  lintOnSave: false,
  productionSourceMap: false,

  devServer: {
    port: 9970,
    proxy: {
      "/api": {
        target: process.env.VUE_APP_PROXY_API_ROOT,
        pathRewrite: {
          "^/api": "",
        },
        router: (req) => {
          return /^\s*VUE_APP_PROXY_API_ROOT=(\S*)/m.exec(fs.readFileSync(".env.development", "utf-8"))[1];
        },
      },
    },
    setupMiddlewares(middlewares, devServer) {
      if (process.env.__EDITOR) {
        process.versions.webcontainer = true;
        process.env.EDITOR = process.env.__EDITOR;
      }
      return middlewares;
    },
  },
  configureWebpack: (config) => {
    // config.plugins.push(smp);
    if (activitiConfig.enable) {
      config.plugins.push({
        apply: (compiler) => {
          compiler.hooks.afterPlugins.tap("afterPlugins", () => {
            activitiConfig.replaceContextRoot(activitiConfig[process.env.VUE_APP_ENV]);
          });
        },
      });
    }
    if (process.env.VUE_APP_CDN === "ON") {
      const externals = {};
      cdnDependencies.forEach((pkg) => {
        externals[pkg.name] = pkg.library;
      });
      config.externals = externals;
    }
  },
  chainWebpack: (config) => {
    config.plugin("copy").tap((args) => {
      if (!activitiConfig.enable) {
        args[0].patterns[0].globOptions.ignore.push("**/activiti/**");
      }
      return args;
    });
    config.optimization.minimizer("terser").tap((args) => {
      args[0].terserOptions.compress.drop_debugger = true;
      args[0].terserOptions.compress.drop_console = true;
      return args;
    });
    const oneOfsMap = config.module.rule("scss").oneOfs.store;
    oneOfsMap.forEach((item) => {
      item
        .use("sass-resources-loader")
        .loader("sass-resources-loader")
        .options({
          resources: ["./src/assets/styles/resources/*.scss"],
        })
        .end();
    });
    config.module.rule("svg").exclude.add(path.join(__dirname, "src/assets/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(path.join(__dirname, "src/assets/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();
    config.plugin("html").tap((args) => {
      args[0].title = process.env.VUE_APP_TITLE;
      if (process.env.VUE_APP_CDN === "ON") {
        args[0].cdn = {
          css: cdnDependencies
            .reduce((acc, cur) => {
              if (Array.isArray(cur._css)) {
                acc = acc.concat(cur._css);
              } else {
                acc.push(cur._css);
              }
              return acc;
            }, [])
            .filter(Boolean),
          js: cdnDependencies
            .reduce((acc, cur) => {
              if (Array.isArray(cur._js)) {
                acc = acc.concat(cur._js);
              } else {
                acc.push(cur._js);
              }
              return acc;
            }, [])
            .filter(Boolean),
          // .map((e) => (process.env.NODE_ENV === "development" ? e.replace(".min", "") : e)),
        };
      }
      return args;
    });
  },
});
